package com.mc.moring.main.modle

import com.mc.moring.app.LogUtils
import com.mc.moring.base.BaseModle
import com.mc.moring.base.HttpCallback
import com.mc.moring.netconfig.Apis
import com.mc.moring.network.HttpResult
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody

class MianModle : BaseModle() {

    private val coroutineScope = CoroutineScope(Dispatchers.IO)

    companion object {
        val instant by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            MianModle()
        }
    }

    fun getZiLiPageProducts(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.getZiLiPageProducts(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    suspend fun getProducts(map: MutableMap<String, String>):  HttpResult<String> {
        return try {
            var result = ""
            val mapString = getZiLiPageJsonNew(map, false)
            val request: RequestBody =
                mapString.toRequestBody("application/json".toMediaTypeOrNull())
            val data = Apis.apiMianServer.getZiLiPageProducts(request)
            result = decryptingZiLiPageData(data)
            LogUtils.logE("e ：$result")
            HttpResult.Success(result)
        } catch (e: Exception) {
            LogUtils.logE("e ：$e")
            HttpResult.Failure(e)
        }
    }

    suspend fun getVersion(map: MutableMap<String, String>): HttpResult<String> {

        return try {
            var result = ""
            val mapString = getZiLiPageJsonNew(map, false)
            val request: RequestBody =
                mapString.toRequestBody("application/json".toMediaTypeOrNull())
            val data = Apis.apiMianServer.getVersion(request)
            result = decryptingZiLiPageData(data)
            LogUtils.logE("e ：$result")
            HttpResult.Success(result)
        } catch (e: Exception) {
            LogUtils.logE("e ：$e")
            HttpResult.Failure(e)
        }

    }

    suspend fun getZiLiPageGeoIpResult(map: MutableMap<String, String>): HttpResult<String> {
        return try {
            var result = ""
            val mapString = getZiLiPageJson(map, false)
            val request: RequestBody =
                mapString.toRequestBody("application/json".toMediaTypeOrNull())
            val data = Apis.apiMianServer.getZiLiPageGeoIpResult(request)
            result = decryptingZiLiPageData(data)
            LogUtils.logE("e ：$result")
            HttpResult.Success(result)
        } catch (e: Exception) {
            HttpResult.Failure(e)
        }
    }

    fun utilsubInfo(map: MutableMap<String, Any>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.utilsubInfo(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun loanSecurityevaluationGuide(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.loanSecurityevaluationGuide(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun subAppNotification(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.subAppNotification(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun preCreditapply(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.preCreditapply(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getCreditInfoForHomePage(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.getCreditInfoForHomePage(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    suspend fun preCreditgetResult(map: MutableMap<String, String>): HttpResult<String> {

        return try {
            var result = ""
            val mapString = getZiLiPageJsonNew(map, false)
            val request: RequestBody =
                mapString.toRequestBody("application/json".toMediaTypeOrNull())
            val data = Apis.apiMianServer.preCreditgetResult(request)
            result = decryptingZiLiPageData(data)
            LogUtils.logE("e ：$result")
            HttpResult.Success(result)
        } catch (e: Exception) {
            LogUtils.logE("e ：$e")
            HttpResult.Failure(e)
        }
    }

    fun subRestShortMsgInfo(map: MutableMap<String, Any>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.subRestShortMsgInfo(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("user info ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getUserInfo(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.getUserInfo(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("user info ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun securitygetApplicationConfig(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.securitygetApplicationConfig(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("getApplicationConfig ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun securitygetBannerConfig(map: MutableMap<String, String>, httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiMianServer.securitygetBannerConfig(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }
}